#!/usr/bin/env bash

#get this script real directory
SOURCE="$0"
while [ -h "$SOURCE"  ]; do # resolve $SOURCE until the file is no longer a symlink
    this_script_dir="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"
    SOURCE="$(readlink "$SOURCE")"
    # if $SOURCE was a relative symlink, 
    #we need to resolve it relative to the path 
    #where the symlink file was located
    [[ $SOURCE != /*  ]] && SOURCE="$this_script_dir/$SOURCE"
done
this_script_dir="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"


if [ `uname` != "Darwin" ]; then
    echo "-- please execute this sh on Mac OS!"
    exit 0
fi

log_level=${log_level:=info}

assist_dir=~/git/tool_sh/assist
source $assist_dir/global.sh
cur_dir=$(pwd)

git_dirs=""
total_of_git_dir=0

get_all_git_dirs()
{
    local cur_dir=`pwd`
    local git_home=`cd $1 && pwd`
    printf "scan dirtory "
    for dir in $(ls -F "$git_home" | grep /)
    do
        cd $git_home
        dir=${dir%/}

        if [ -d "$dir/.git" ];then
            cd $git_home/$dir
            current_branch=`git name-rev --name-only HEAD`

            #current branch 's remote rep name
            remote_rep=`git config --get branch.${current_branch}.remote`

            git remote -v | grep "$remote_rep" | grep -E "github|gitlab|gitee|codeup.aliyun.com:5eaa51f238076f00011bc3a2" > /dev/null

            if [ $? -eq 0 ]; then
                continue
            fi

            total_of_git_dir=${#git_dirs[*]}
            git_dirs[$total_of_git_dir]="$git_home/$dir"
            printf "."
        fi
    done
    printf "\n"
    cd $cur_dir
}


#$1:git directory
get_git_info()
{
    #return variable: current_branch, remote_rep, remote_branch, git_log_id
    local cur_dir=`pwd`
    cd "$1"

    #current_branch=`git name-rev --name-only HEAD`
    #current_branch=`git branch -vv | awk '$1=="*"{print $2}'`
    local info=`git branch -vv | awk '$1=="*"{print $2 " " $4}'`
    #vlogi "info:$info"   # info=master origin master
    current_branch=${info% [*}
    local remote_rep_branch=${info#* [}
    #trim []
    remote_rep_branch=${remote_rep_branch#[}
    remote_rep_branch=${remote_rep_branch%]}
    remote_rep_branch=${remote_rep_branch%:}

    remote_rep=${remote_rep_branch%/*} #current branch 's remote rep name
    remote_branch=${remote_rep_branch#*/}

    local log_last=`git log | head -n 1`
    git_log_id=${log_last#* }
    git_log_id=${git_log_id:0:8 }

    #echo "$current_branch  $remote_rep $remote_branch $git_log_id"

    cd $cur_dir
}

push()
{
    local git_home=$1
    local cur_dir=`pwd`
    index=0;
    for dir in ${git_dirs[@]}
    do
        let ++index
        dir_name=`basename $dir`
        cd $dir
        get_git_info ./ 

        status=`git status -s`
        local tag=""
        if [ -n "$status" ] ; then
            tag="*"
        fi
            
        #echo "--------------------------$index/$total_of_git_dir---$remote_rep.$remote_branch-------------------------"
        printf "%2d/%d git push [%-18s] %8s -> %-16s %s\n" $index $total_of_git_dir "$dir_name" "$current_branch" "$remote_rep.$remote_branch" "$tag"

        if [ -n "$status" ] ; then

            git add . > /dev/null
            git commit -m"$(date +"%Y-%m-%d %H:%M:%S") commited by push_all.sh" > /dev/null

            git push > /dev/null 2>&1 || (git push ; continue)
            git push --tags > /dev/null 2>&1 || (git push ; continue)
        fi

        #git remote | grep qq > /dev/null
        #if [ $? -eq 0 ] ; then #found
        #    printf "%2d/%d git push [%-18s] %8s -> %-16s\n" $index $total_of_git_dir "$dir_name" $current_branch qq.master
        #    git push qq > /dev/null 2>&1 || (git push ; continue)
        #fi
    done
    cd $cur_dir
}


main()
{
    cd $this_script_dir
    get_all_git_dirs ~/git
    push ~/git
    return 0;
}

main || exit 1
exit 0



